/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xui_lib.component.textview.autoFit;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.WeakHashMap;

/**
 * 能自动将子类中的TextView自适应大小
 *
 * @author xuexiang
 * @since 2019-05-14 22:38
 */
public class AutoFitLayout extends StackLayout {
    private boolean mEnabled;
    private int mMinTextSize;
    private float mPrecision;
    private WeakHashMap<Component, AutoFitHelper> mHelpers = new WeakHashMap<>();

    /**
     * 构造器
     *
     * @param context
     */
    public AutoFitLayout(Context context) {
        super(context);
        init(context, null);
    }

    /**
     * 构造器
     *
     * @param context
     * @param attrs
     */
    public AutoFitLayout(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    /**
     * 构造器
     *
     * @param context
     * @param attrs
     * @param styleName
     */
    public AutoFitLayout(Context context, AttrSet attrs, String styleName) {
        super(context, attrs, styleName);
        init(context, attrs);
    }

    private void init(Context context, AttrSet attr) {
        boolean enableFit = true;
        int minTextSize = -1;
        float precision = -1;

        if (attr != null) {
            if (attr.getAttr("aftv_enable").isPresent()) {
                enableFit = attr.getAttr("aftv_enable").get().getBoolValue();
            }
            if (attr.getAttr("aftv_minTextSize").isPresent()) {
                minTextSize = attr.getAttr("aftv_minTextSize").get().getIntegerValue();
            }
            if (attr.getAttr("aftv_precision").isPresent()) {
                precision = attr.getAttr("aftv_precision").get().getFloatValue();
            }
        }
        mEnabled = enableFit;
        mMinTextSize = minTextSize;
        mPrecision = precision;
    }

    @Override
    public void addComponent(Component child, int index, ComponentContainer.LayoutConfig layoutConfig) {
        super.addComponent(child, index, layoutConfig);
        if (child instanceof Text) {
            Text textView = (Text) child;
            AutoFitHelper helper = AutoFitHelper.create(textView).setEnabled(mEnabled);
            if (mPrecision > 0) {
                helper.setPrecision(mPrecision);
            }
            if (mMinTextSize > 0) {
                helper.setMinTextSize(mMinTextSize);
            }
            mHelpers.put(textView, helper);
        }
    }

    /**
     * getAutofitHelper
     *
     * @param textView
     * @return AutoFitHelper
     * @ Returns the {@link AutoFitHelper} for this child View.
     */
    public AutoFitHelper getAutofitHelper(Text textView) {
        return mHelpers.get(textView);
    }

    /**
     * getAutofitHelper
     * Returns the {@link AutoFitHelper} for this child View.
     *
     * @param index
     * @return AutoFitHelper
     */
    public AutoFitHelper getAutofitHelper(int index) {
        return mHelpers.get(getComponentAt(index));
    }
}
